<?php

require_once( '../conf/conf.inc' );
require_once( PATH_FRAME.'/etwork.php' );

$etw = new etwork();
$etw->parseURL('member', 1);

if (!isset($etw->pathParameters[0])) {

    //ログイン済みならマイページへリダイレクト
    if ( $etw->isLogin() ) Access::jump(URL_SITE . 'member/' . $etw->getUserId());

    $etw->setTemplate('guest');
    $etw->setTemplateGroup('member');


} else if ($etw->pathParameters[0] == 'login') {
    
    //ログイン済みならマイページへリダイレクト
    if ( $etw->isLogin() ) Access::jump(URL_SITE . 'member/' . $etw->getUserId());
    
    $etw->setTemplate('login');
    $etw->setTemplateGroup('member');
    
    $etw->noCaching = true;
    
    if (isset($etw->EX_POST['login'])) {

        $etw->jumpTo = URL_SITE . 'member/';
        
        $loader = new modelLoader;
        $loader->model = 'member';
        $loader->isEdit = true;
        $etw->addLoader($loader);

        $etw->loadData();
    }


} else if ($etw->pathParameters[0] == 'logout') {

    User::logoutAccount();
    Access::jump(URL_SITE);

} else if ($etw->pathParameters[0] == 'edit_login') {

    $etw->limitLevel = access::MEMBER;
    $etw->jumpTo = URL_SITE . 'member/';
    
    $etw->setTemplate('edit.login', 1);
    $etw->setTemplateGroup('member');
    $etw->addClearTemplate( null, 'member|solo'.$etw->getUserId());
    
    $loader = new modelLoader;
    $loader->model = 'member';
    $loader->targetCustomFunction = 'findByMail';
    $loader->targetId = 'login_mail';
    $loader->solo = @$etw->EX_POST['login_mail'];
    $loader->isEdit = true;
    $etw->addLoader($loader);

    $loader = new modelLoader;
    $loader->model = 'member';
    $loader->targetCustomFunction = 'editLogin';
    $loader->targetChoiceFunction = 'findById';
    $loader->targetId = 'member_id';
    $loader->solo = $etw->getUserId();
    $loader->isEdit = true;
    $etw->addLoader($loader);

    $etw->loadData();

} else if ($etw->pathParameters[0] == 'edit_profile') {

    $etw->limitLevel = access::MEMBER;
    $etw->jumpTo = URL_SITE . 'member/';
    
    $etw->setTemplate('edit.profile', 1);
    $etw->setTemplateGroup('member');
    $etw->addClearTemplate( null, 'member|solo'.$etw->getUserId());
    
    
    //基本プロフィール
    $loader = new modelLoader;
    $loader->model = 'member';
    $loader->targetCustomFunction = 'editProfile';
    $loader->targetChoiceFunction = 'findById';
    $loader->targetId = 'member_id';
    $loader->solo = $etw->getUserId();
    $loader->isEdit = true;
    $loader->formParameters = array('name','note','_birthday');
    $etw->addLoader($loader);

    //カテゴリ
    $loader = new modelLoader;
    $loader->model = 'category';
    $loader->targetCustomFunction = 'updatePostCategory';
    $loader->targetChoiceFunction = 'findParameterByPost';
    $loader->returning = 'list';
    $loader->isEdit = true;
    $loader->solo = $etw->getUserId();
    $loader->mapperParameters = array(
        'member' => 1
    );
    $loader->formParameters = array('_category');
    $etw->addLoader($loader);

    //カテゴリフォーム
    $loader = new modelLoader;
    $loader->model = 'category';
    $loader->targetId = 'slug';
    $loader->solo = 'hito';
    $loader->targetChoiceFunction = 'findByPost';
    $loader->returning = 'listform';
    $etw->addLoader($loader);

    $etw->loadData();

} else if ($etw->pathParameters[0] == 'edit') {

    //未ログインならメンバートップへ遷移
    if (! $etw->isLogin() ) Access::jump(URL_SITE . 'member');

    $etw->setTemplate('mypage');
    $etw->setTemplateGroup('member');

} else if (isset($etw->pathParameters[0]) && isNumber($etw->pathParameters[0])) {

    $etw->setTemplate( 'member', false, false, 'member|solo'.$etw->pathParameters[0]);
    
    if (! $etw->isCached() ) {
        $loader = new modelLoader;
        $loader->model = 'member';
        $loader->solo = $etw->pathParameters[0];
        $etw->addLoader($loader);
        $etw->loadData();
    }
}

$etw->showFinal();


